package com.cy.ext.plugin;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-11-21
 * Time: 下午8:20
 * To change this template use File | Settings | File Templates.
 */
public class CLMPRestful implements Interceptor {

    private static final String isRestfulForwardKey = "_isRestfulForward_";
    private Set<String> set = new HashSet<String>() {
        private static final long serialVersionUID = 2717581127375143508L;

        {
            // add edit 与  JFinal 原有规则相同
            add("show");
            add("save");
            add("update");
            add("delete");
        }
    };

    /**
     * add  edit 无需处理
     * <p/>
     * GET		/user			--->	index
     * GET		/user/id		--->	show
     * GET		/user/get_xxx	--->	xxx
     * POST		/user			--->	save
     * POST		/user/id		--->	update
     * POST		/user/post_xxx	--->	xxx
     * DELECT	/user/id		--->	delete
     */
    public void intercept(ActionInvocation ai) {
        // 阻止 JFinal 原有规则 action 请求
        Controller controller = ai.getController();
        Boolean isRestfulForward = controller.getAttr(isRestfulForwardKey);
        String methodName = ai.getMethodName();
        if (set.contains(methodName) && isRestfulForward == null) {
            ai.getController().renderError(404);
            return;
        }

        if (isRestfulForward != null && isRestfulForward) {
            ai.invoke();
            return;
        }

        String controllerKey = ai.getControllerKey();
        String method = controller.getRequest().getMethod().toUpperCase();
        String urlPara = controller.getPara();
        if ("GET".equals(method)) {
            if (urlPara != null && !"edit".equals(methodName)) {
                if (urlPara.startsWith("get_")) {
                    controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
                    controller.forwardAction(controllerKey + "/" + urlPara.replaceFirst("get_", ""));
                    return;
                } else {
                    controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
                    controller.forwardAction(controllerKey + "/show/" + urlPara);
                    return;
                }
            }
        } else if ("POST".equals(method)) {
            if (urlPara != null) {
                if (urlPara.startsWith("post_")) {
                    controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
                    controller.forwardAction(controllerKey + "/" + urlPara.replaceFirst("post_", ""));
                    return;
                } else {
                    controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
                    controller.forwardAction(controllerKey + "/update/" + urlPara);
                    return;
                }
            } else {
                controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
                controller.forwardAction(controllerKey + "/save");
                return;
            }
        } else if ("PUT".equals(method)) {
            controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
            controller.forwardAction(controllerKey + "/update/" + urlPara);
            return ;
        }else if ("DELETE".equals(method)) {
            controller.setAttr(isRestfulForwardKey, Boolean.TRUE);
            controller.forwardAction(controllerKey + "/delete/" + urlPara);
            return;
        }

        ai.invoke();
    }
}